# 导入蓝图包
import os
from flask import Blueprint,request,jsonify
import json

from data import get_data

# 创建蓝图
bp_course = Blueprint('course',__name__)
from db import db1

# 接口
@bp_course.route('/couponM',methods=['POST','GET','PUT','DELETE'])
def couponM():
    # 获取数据
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        # 构造SQL语句
        sql = """insert into coupon values(0,'%s','%d','%s','%s')"""%(data['name'],int(data['total']),data['starttime'],data['endtime'])
        # db添加
        db1.intsert(sql)
        # 返回结果
        return '添加成功'
    
    # 获取
    elif request.method == 'GET':
        # 根据id获取出数据
        id = request.args.get('id')
        sql = """select * from coupon where id=%d"""%(int(id))
        res = db1.find(sql)
        return jsonify({'code':200,'coupon':res})

    # 修改
    elif request.method == 'PUT':
        # 获取用户输入的数据
        data = request.data
        if  data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        # 构造sql语句  update 表名 set 字段名1=值,字段名2=值 where id=1
        sql = """update coupon set name='%s',total=%d,starttime='%s',endtime='%s' where id=%d"""%(data['name'],int(data['total']),data['starttime'],data['endtime'],int(data['id']))
        print(sql)
        db1.intsert(sql)
        return jsonify({'code':200})

    # 删除
    elif request.method == 'DELETE':
        # 获取id
        id = request.args.get('id')
        print(id)
        # 删除获取到的id的数据
        sql = "delete from coupon where id=%d"%(int(id))
        print(sql)
        # 删除方法
        db1.intsert(sql)
        return jsonify({'code':200})

# section部门表
@bp_course.route('/section',methods=['POST','GET','PUT','DELETE'])
def section():
    if request.method == 'POST':
        # 获取传过来的数据
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        # 构造sql 语句
        sql = """insert into section values(0,"%s",%d)"""%(data['name'],int(data['sum']))
        print(sql)
        # db添加
        db1.intsert(sql)
        # 返回结果
        return jsonify({'code':200})

    # 获取数据
    elif request.method == 'GET':
        data = request.form
        sql = """select * from section"""
        res = db1.fingall(sql)
        return jsonify({'code':200,'sections':res})

    # 修改
    elif request.method == 'PUT':
                # 获取用户输入的数据
        data = request.data
        if  data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        # 构造sql语句  update 表名 set 字段名1=值,字段名2=值 where id=1
        sql = """update section set name="%s",sum=%d where id=%d"""%(data['name'],int(data['sum']),int(data['id']))
        print(sql)
        db1.intsert(sql)
        return jsonify({'code':200})

 
    # 删除数据
    elif request.method == 'DELETE':
        id = request.args.get('id')
        sql = """delete from staff where id=%d"""%(int(id))
        db1.intsert(sql)
        return jsonify({'code':200})

# staff员工表
@bp_course.route('/staff',methods=['POST','GET','PUT','DELETE'])
def staff():
    if request.method == 'POST':
        # 获取数据
        data = get_data()
        # 构造sql语句
        #   select * from 表1 left join 表2 on 表1的id = 表2的外键  where id = data['pid']
        sql1 = '''select * from section where id = %d '''%(int(data['pid']))
        res = db1.find(sql1)
        print(res)
        sum = int(res['sum'])+1
        sql2 = 'update section set sum = %d where id = %d '%(sum,int(data['pid']))
        db1.intsert(sql2)
        sql = """insert into staff values(0,"%s","%s","%d","%d")"""%(data['name'],data['gender'],int(data['salary']),int(data['pid']))
        # db添加
        db1.intsert(sql)
        # 返回结果
        return jsonify({'code':200})
    # 获取出所有的数据
    elif request.method == 'GET':
        data = request.form
        sql = """select * from staff"""
        res = db1.fingall(sql)
        return jsonify({'code':200,'sections':res})

    # elif request.method == 'PUT':
        # 获取出来数据
        # id = request.args.get('id')

    # 修改
    elif request.method == 'PUT':
                # 获取用户输入的数据
        data = request.data
        if  data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        # 构造sql语句  update 表名 set 字段名1=值,字段名2=值 where id=1
        sql = """update staff set name="%s",gender="%s",salary="%s",pid=%d where id=%d"""%(data['name'],data['gender'],data['salary'],int(data['pid']),int(data['id']))
        print(sql)
        db1.intsert(sql)
        return jsonify({'code':200})

    # 删除数据
    elif request.method == 'DELETE':
        id = request.args.get('id')
        sql = """delete from staff where id=%d"""%(int(id))
        db1.intsert(sql)
        return jsonify({'code':200})

# 导入myfile文件里面的myfile方法
from utils.myfile import myfile
# 创建文件接口
@bp_course.route('/upload',methods=['POST'])
def upload():
    # 获取文件
    file = request.files['file']
    # 选择文件夹

    # 创建文件夹   文件夹的名称
    myfile.file_mkdir(3)
    # 保存文件                    文件夹的路径
    file.save(os.path.join('./static/upload/3/',file.filename))
    # 保存 返回ok
    return 'ok'

# 景区类型操作
@bp_course.route('/spot',methods=['POST','GET','PUT','DELETE'])
def spot():
    # 添加景区类型
    if request.method == 'POST':
        # 获取数据
        data = get_data()
        # 如果获取出来了数据
        if data:
            # 构造sql语句
            sql = """insert into spot values(0,"%s")"""%(data['name'])
            # 添加
            db1.intsert(sql)
            return jsonify({'code':200})
    # 修改数据
    
    elif request.method == 'PUT':
        pass
    # 删除数据

    elif request.method == 'DELETE':
        pass
    
    # 获取数据
    else:
        # 查看所有数据
        sql = """select * from spot"""
        db1.fingall(sql)
        return jsonify({'code':200})


# 景点操作
@bp_course.route('/scenic',methods=['POST','GET','PUT','DELETE'])
def scenic():
    # 添加
    if request.method == 'POST':
        # 获取数据
        data = request.form
        # 如果获取出来了
        if data:
            # 构造sql语句
            sql = """insert into scenic values("%s",%.2f,%d)"""%(data['name'],float(data['price']),int(data['pid']))
            # 添加
            db1.intsert(sql)
            # 获取新加的id
            sql1 = """select id from scenic where name='%s'"""%(data['name'])
            res = db1.find(sql)
            # 文件上传,上传成功后图片路径
            file = request.files['file']
            myfile.file_mkdir(str(['id']))
            file.save(os.path.join('./static/upload/'+str(res['id']),file.filename))                                                                                                                                                                                                                                                                                                                                         
            #组装数据添加
            sql3 = "update scenic set pic='%s' where id=%d"%('/static/upload/'+str(res['id'])+"/"+file.filename,int(res['id']))
            db1.insert(sql3)
            return jsonify({"code":200})
        else:
            sql = 'select * from scenic'
            res = db1.fingall(sql)
            return jsonify({"code":200,'list':res})


    # 修改数据
    elif request.method == 'PUT':
        pass

    # 删除数据
    elif request.method == 'DELETE':
        pass

    # 获取数据
    else:
        sql = """select * from scenic"""
        db1.fingall(sql)
        return jsonify({'code':200})
